<?php 
/**
 * 显示已经完成的活动
 */
class PhotoAction extends HomeAction{
public function index(){
		$Activity = M('Activity');
		$field = "activity.*,user.username,user.sex";
		$where = "1 AND activity.state=1";
		if($_GET['keyword']){
			$where .= " AND activity.name LIKE '%{$_GET['keyword']}%'";
		}
		$order = "activity.create_time DESC";
		$limit = C('PAGE_COUNT');
		$count = $Activity->where($where)->count();
		$Page = new Page($count, C('PAGE_COUNT'));
		$limit = "{$Page->firstRow},{$Page->listRows}";
		$join = "LEFT JOIN `user` ON user.uid=activity.uid";
		$list = $Activity->where($where)->limit($limit)->join($join)->field($field)->order($order)->select();
		$Participation = M('participation');
		foreach($list as &$v){
			$tmp = json_decode($v['scenic_images']);
			$v['thumb'] = $tmp[0];
			$v['images']=array_slice($tmp,0,3);
			$where="uid={$_SESSION['uid']} AND acid={$v['id']}";
			$count = $Participation->where($where)->count();
			if($count){	$v['if_reg']=1;	}else{$v['if_reg']=0;}
			/**
			 * 获取已经报名人员男女比例
			 */
			$field = "count(*) as `count`,user.sex";
			$where="acid = {$v['id']}";
			$group="user.sex";
			$join = "LEFT JOIN `user` ON user.uid=participation.uid";
			$proportion = (array)$Participation->where($where)->field($field)->join($join)->group($group)->select();
			$v['reg_man']=$v['reg_woman']=0;
			foreach($proportion as $v1){
				switch($v1['sex']){
					case '1':
						$v['reg_woman']=$v1['count'];
						break;
					case '0':
						$v['reg_man']=$v1['count'];
						break;
				}
			}
		}
		$this->assign('page',$Page->show());
		$this->assign('list',$list);
		$this->display();
	}
	public function view(){
		$id = $_REQUEST['id'];
		$Comment = M('comment');
		if($_GET){
			$Activity = M('Activity');
			$activity_info = $Activity->where("id={$id}")->find();
			$activity_info['picture_urls'] = json_decode($activity_info['picture_urls']);
			$this->assign('activity_info',$activity_info);
			$comment_list = $Comment->where("acid='$id'")->join("LEFT JOIN user ON user.uid=comment.uid")->field("comment.*,user.username")->select();
			$this->assign('comment_list',$comment_list);
			$this->display();
		}elseif($_POST){
			if(!Session::get('uid')){
				$this->assign('jumpUrl',U('User/login'));
				$this->error('请您登录之后在发');
			}
			$data['acid'] = $_POST['id'];
			$data['content'] = $_POST['content'];
			$data['uid']	= Session::get('uid');
			$data['create_time']  = $_SERVER['REQUEST_TIME'];
			$this->assign('jumpUrl',$_SERVER['HTTP_REFERER']);
			$Comment->add($data);
			$this->success("评论成功");
		}
	}
}
?>